package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.qiniu.QiniuUtils;
import com.fram.entity.SysAdvertisingDisplay;
import com.fram.mapper.SysAdvertisingDisplayMapper;
import com.fram.service.admin.SysAdvertisingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SysAdvertisingServiceImpl implements SysAdvertisingService {

	@Autowired
	private SysAdvertisingDisplayMapper sysAdvertisingDisplayMapper;

	@Autowired
	private QiniuUtils qiniuUtils;

	/** **/
	@Override
	public PageInfo<SysAdvertisingDisplay> list(PageBean<SysAdvertisingDisplay> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<SysAdvertisingDisplay> list = sysAdvertisingDisplayMapper.listAdvertising(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	/** **/
	@Override
	public SysAdvertisingDisplay details(Long id) throws ProException {
		SysAdvertisingDisplay request = sysAdvertisingDisplayMapper.selectByPrimaryKey(id);
		if (request == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}

	/** **/
	@Override
	public void add(SysAdvertisingDisplay request) throws ProException {
		if (!StringUtils.isEmpty(request.getDisplayUrl())) {
			request.setDisplayUrl(qiniuUtils.uploadImageUrl(request.getDisplayUrl()));
		}
		if (!StringUtils.isEmpty(request.getCoverUrl())) {
			request.setCoverUrl(qiniuUtils.uploadImageUrl(request.getCoverUrl()));
		}
		if (sysAdvertisingDisplayMapper.insertSelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		// } catch (DataAccessException e) {
		// throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(), ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		// }

	}

	/** **/
	@Override
	public void update(SysAdvertisingDisplay request) throws ProException {
		if (!StringUtils.isEmpty(request.getDisplayUrl())) {
			request.setDisplayUrl(qiniuUtils.uploadImageUrl(request.getDisplayUrl()));
		}
		if (!StringUtils.isEmpty(request.getCoverUrl())) {
			request.setCoverUrl(qiniuUtils.uploadImageUrl(request.getCoverUrl()));
		}
		if (sysAdvertisingDisplayMapper.updateByPrimaryKeySelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void delete(Long id) throws ProException {
		SysAdvertisingDisplay advertisingDisplay = sysAdvertisingDisplayMapper.selectByPrimaryKey(id);
		if (advertisingDisplay == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		qiniuUtils.delete(advertisingDisplay.getDisplayUrl());
		if (sysAdvertisingDisplayMapper.deleteByPrimaryKey(id) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
}
